package love.yuanmengrobot.attendancesystem.servlet;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/6/20 16:50
 */

import love.yuanmengrobot.attendancesystem.dao.AttendanceDao;
import love.yuanmengrobot.attendancesystem.entity.Attendancemain;

import java.io.IOException;
import java.io.PrintWriter;

public class AttendanceServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String op = request.getParameter("op");
        String atname = request.getParameter("atname");
        String clazzid = request.getParameter("clazzid");
        String memberid = request.getParameter("memberid");
        String attype = request.getParameter("attype");
        String key = request.getParameter("key");
        System.out.println(atname + clazzid + memberid + attype + key);

        Attendancemain attendancemain = new Attendancemain();
        attendancemain.setAtname(atname);
        attendancemain.setClazzid(clazzid);
        attendancemain.setMemberid(memberid);
        attendancemain.setAttype(attype);
        attendancemain.setKey(key);

        PrintWriter pw = response.getWriter();

        AttendanceDao attendanceDao = new AttendanceDao();
        int su = 0;
//        pw.print("您使用GET方式请求该Servlet。<br />" + "name = " + name + ",age = " + age);
        //插入数据到：签到头表
        if ("start".equals(op)) {
            //处理发起签到请求
            System.out.println("start");

            su = attendanceDao.insertMain(attendancemain);
            if (su == 1) {
                pw.print("{\"meg\":\"发起成功\"}");
            } else {
                pw.print("{\"meg\":\"发起失败\"}");
            }


        } else {
            //处理结束签到请求
            System.out.println("end");
            su = attendanceDao.updateMain(atname, clazzid);
            if (su == 1) {
                pw.print("{\"meg\":\"结束成功\"}");
            } else {
                pw.print("{\"meg\":\"结束失败\"}");
            }
        }


        pw.flush();
        pw.close();


    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String key = request.getParameter("key");
//        String clazzid = request.getParameter("clazzid");
        String memberid = request.getParameter("memberid");
//        String address = request.getParameter("address");
        String address = "";
//        Attendance?atname=第一批次&clazzid=11&memberid=1&address=北京
        int su = 0;
        //学生请求签到
        {

            AttendanceDao attendanceDao = new AttendanceDao();
            su = attendanceDao.selectAtidAndEndTime(key, memberid, address);
        }

        PrintWriter pw = response.getWriter();
        if(su==1){
            pw.print("{\"meg\":\"签到成功\"}");
        }else
        {
            pw.print("{\"meg\":\"签到失败\"}");
        }
//        pw.print("您使用POST方式请求该Servlet。<br />" + "name = " + name + ",age = " + age);
        pw.flush();
        pw.close();
    }

}
